import Taro, { Component } from '@tarojs/taro'
import { View, Text, Button, Image, ScrollView, Textarea } from '@tarojs/components'
import './index.less'
import { queryFavorite } from '../../utils'

import Dialog from '../../components/Dialog'

export default class Index extends Component {
  state = {
    json: '',
    show: false
  }
  componentWillMount() { }


  componentDidMount() {

  }

  componentWillUnmount() { }

  componentDidShow() { }

  componentDidHide() { }
  clearCache = () => {
    Taro.setStorageSync("local_footprint",[])
    // Taro.setStorageSync("local_favorite",[])
    Taro.showToast({
      title: '成功清除足迹',
      icon: 'success'
    })
  }
  aboutUs = () => {
    Taro.navigateTo({
      url: '/pages/about/index'
    })
  }
  upDateHistory = () => {
    Taro.navigateTo({
      url: '/pages/history/index'
    })
  }
  exportConfig() {
    let rss = Taro.getStorageSync('my_subscribe');
    if (rss && rss.length > 0) {
      Taro.setClipboardData({
        data: JSON.stringify(rss),
        success: function () {
          // wx.hideToast();
          Taro.showToast({
            icon: 'success',
            title: '导出信息已复制到剪贴板'
          })
          // showModal('导出信息已复制到剪贴板')
        }
      })
    } else {
      Taro.showToast('没有订阅信息导出！');
    }
  }
  toList(source) {
    Taro.navigateTo({
      url: '/pages/list/index?source=' + source
    })
  }
  config = {
    // navigationBarTitleText: '',
    navigationStyle: 'custom',
  }
  onClose() {
    this.setState({
      show: false
    })
  }
  onShareAppMessage() {
    return {
      title: 'RSS阅读器',
      desc: '提升阅读效率',
      imageUrl: require('../../assets/img/title.jpg'),
      path: '/pages/index/index'
    }
  }
  onSave() {
    let list = Taro.getStorageSync('my_subscribe');
    let rss = [];
    try {
      rss = JSON.parse(this.state.json);
    } catch (error) {
      console.log('error', this.state.json)
      return false;
    }
    rss.forEach(v => {
      console.log('sss',)
      let index = list.findIndex(value => value.url == v.url)
      //  =  findArrayIndex(this.mySubscribe,v);
      if (index == -1) {
        list.push(v)
      }

    });
    Taro.setStorageSync('my_subscribe', list);
  }
  render() {
    return (
      <ScrollView className='scrollPage'>
        <Dialog
          onClose={() => this.onClose()}
          title='导入' show={this.state.show}
        >

          <Textarea
            value={this.state.json} onInput={(e) => this.setState({
              json: e.target.value
            })}
            className='textarea' placeholder='请输入'
          />


          <View className='cu-bar bg-white justify-end '>
            <View className='action'>
              <Button onClick={() => this.onClose()} className='cu-btn text-green margin-left'>取消</Button>
              <Button onClick={() => this.onSave()} className='cu-btn bg-green margin-left'>确定</Button>
            </View>
          </View>



        </Dialog>
        
        <View className='header'>
          <Image className='header-bg' src={require('../../assets/img/shuji.jpg')} ></Image>
          <View className='header-info'>
            <View className='avatar' hover-class='none' hover-stop-propagation='false'>
              <open-data type='userAvatarUrl' />
            </View>
            <View className='nickName' hover-class='none' hover-stop-propagation='false'>
              <open-data type='userNickName' />
            </View>
          </View>
        </View>
        <View className='cu-bar bg-white solid-bottom margin-top'>
          <View className='action'>
            <Text className='cuIcon-titles lg text-green'></Text>个人
  </View>

        </View>
        <View className='cu-list menu '>

          <View onClick={() => this.toList('favorite')} className='cu-item arrow'>
            <View className='content'>
              <Text className='cuIcon-favor lg text-gray'></Text>
              <Text className='text-grey'>本地收藏</Text>
            </View>
          </View>
          <View onClick={() => this.toList('history')} className='cu-item arrow'>
            <View className='content'>
              <Text className='cuIcon-footprint lg text-gray'></Text>
              <Text className='text-grey'>浏览足迹</Text>
            </View></View>


        </View>
        <View className='cu-bar bg-white solid-bottom margin-top'>
          <View className='action'>
            <Text className='cuIcon-titles lg text-green'></Text>备份
  </View>
        </View>

        <View className='cu-list menu'>
          <View onClick={() => this.setState({ show: true })} className='cu-item arrow'>
            <View className='content'>
              <Text className='cuIcon-down lg text-gray'></Text>
              <Text className='text-grey'>导入订阅</Text>
            </View>
          </View>
          <View onClick={() => this.exportConfig()} className='cu-item arrow'>
            <View className='content'>
              <Text className='cuIcon-upload lg text-gray'></Text>
              <Text className='text-grey'>导出订阅</Text>
            </View>
          </View>
        </View>

        <View className='cu-bar bg-white solid-bottom margin-top'>
          <View className='action'>
            <Text className='cuIcon-titles lg text-green'></Text>系统
  </View>

        </View>
        <View className='cu-list menu '>

          <Button open-type='contact' className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-servicefill lg text-gray'></Text>
              <Text className='text-grey'>联系客服</Text>
            </View>
          </Button>
          <Button open-type='openSetting' className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-settings lg text-gray'></Text>
              <Text className='text-grey'>设置权限</Text>
            </View>
          </Button>


          <Button open-type='feedback' className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-community lg text-gray'></Text>
              <Text className='text-grey'>问题反馈</Text>
            </View>
          </Button>


          <Button open-type='share' className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-share lg text-gray'></Text>
              <Text className='text-grey'>推荐给朋友</Text>
            </View>
          </Button>


          <Button onClick={this.clearCache} className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-exit lg text-gray'></Text>
              <Text className='text-grey'>清除缓存</Text>
            </View>
          </Button>
          <Button onClick={this.upDateHistory} className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-newshot lg text-gray'></Text>
              <Text className='text-grey'>更新历史</Text>
            </View>
          </Button>
          <Button onClick={this.aboutUs} className='cu-item text-left arrow'>

            <View className='content'>
              <Text className='cuIcon-hot lg text-gray'></Text>
              <Text className='text-grey'>关于</Text>
            </View>
          </Button>
        </View>
      </ScrollView>
    )
  }
} 
